package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.ccl.iot.App;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.R;
import com.ccl.iot.bean.ArmPartitionInfoBean;
import com.ccl.iot.privates.BitmapCollector;
import com.ccl.iot.privates.BitmapMask;
import com.ccl.iot.publics.UIUtils;

import static com.ccl.iot.device.IOTSecurityArmConst.EARM_STATE_ALARMING;
import static com.ccl.iot.device.IOTSecurityArmConst.EARM_STATE_ARM;
import static com.ccl.iot.device.IOTSecurityArmConst.EARM_STATE_DISARM;

public class SecurityArmIconProvider extends DeviceIconProvider {

    /*
     * 	Static functions
     */
    public static boolean RegisterProviderType() {
        return RegisterProviderType(SecurityArmIconProvider.class);
    }


    //=========================================modify start=====================================================
    public synchronized static Bitmap Build(int aStyle, int aWidth, int aHeight, int aBackColor,
                                            int state, boolean isPressed, BitmapMask aMask, int iconId) {
        if (aWidth > 0 && aHeight > 0) {
            Bitmap tIcon;
            String tKey = "Probe." + aStyle + "." + aWidth + "x" + aHeight  + ":" + state + "-"
                    + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode()) + "." + iconId;

            tIcon = BitmapCollector.Get(tKey);

            if (tIcon == null) {
                tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT
                        ? Config.RGB_565 : Config.ARGB_8888);

                Canvas tCanvas = new Canvas(tIcon);

                int ids = R.drawable.icon_security_disarm;

                switch (state) {
                    case EARM_STATE_ARM:
                        ids = R.drawable.icon_security_arm;
                        break;
                    case EARM_STATE_DISARM:
                        ids = R.drawable.icon_security_disarm;
                        break;
                    case EARM_STATE_ALARMING:
                        ids = R.drawable.icon_security_arm;
                        break;
                    default:
                        ids = R.drawable.icon_security_disarm;
                        break;
                }

                Bitmap centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ids);

                int size = aWidth > aHeight ? aHeight : aWidth;

                centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);

                float left = (aWidth - centerIcon.getWidth()) / 2;
                float top = (aHeight - centerIcon.getHeight()) / 2;


                if (aBackColor != Color.TRANSPARENT) {
                    tCanvas.drawColor(aBackColor);
                }

                tCanvas.drawBitmap(centerIcon, left, top, new Paint());

                if (aMask != null) {
                    aMask.paintMask(tCanvas);
                }

                tCanvas.save();
                tCanvas.restore();

                BitmapCollector.Add(tKey, tIcon);

                return tIcon;
            }

            return tIcon;
        }

        return null;
    }



    public static Bitmap Build(IOTSecurityArm aDevice, int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing) {
        if (aDevice != null) {
            ArmPartitionInfoBean armPartitionInfoBean = aDevice.getArmPartitionInfoMap().get(0);
            if (armPartitionInfoBean != null) {
                byte state = armPartitionInfoBean.getState();
                return Build(aStyle, aWidth, aHeight, aBackColor, state, aPressing, null, aDevice.getSplitIconId());
            }
            return Build(aStyle, aWidth, aHeight, aBackColor, EARM_STATE_DISARM, aPressing, null, aDevice.getSplitIconId());
        }
        return null;
    }


    /*
     * 	Non-static functions
     */
    public SecurityArmIconProvider(IOTSecurityArm aDevice) {
        super(aDevice);
    }

    @Override
    public Bitmap build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing) {
        return Build((IOTSecurityArm) iDevice, aStyle, aWidth, aHeight, aBackColor, aPressing);
    }

}
